/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/** This class allows to add an entry in the directory */
final class AddAddress extends JDialog {
	/** Language resource */
	private ResourceBundle msgBundle;

	/** Main panel */
	private JPanel panel;

	private JLabel nameLabel;
	private JTextField nameField;
	private JLabel emailLabel;
	private JTextField emailField;

	private JLabel supLabels[];
	private JTextField supFields[];

	private JButton addXML;
	private JButton addLDAP;
	private JButton cancel;

	/** User profile */
	private Profile profile;

	/** Listener for this class */
	private AddAddressListener listener;

	/** Tell wether the address/mail was added or not */
	private boolean added;

	/**
	 * Constructor
	 * 
	 * @param profile
	 *            user profile
	 * @param name
	 *            name to add
	 * @param email
	 *            address to add
	 * @param msgBundle
	 *            language resource
	 */
	protected AddAddress(Profile profile, String name, String email,
			ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("AddAddress.frameTitle"), true);

		this.msgBundle = msgBundle;
		this.profile = profile;

		added = false;

		listener = new AddAddressListener();

		panel = new JPanel();

		nameLabel = new JLabel(msgBundle.getString("AddAddress.nameLabelText"));
		panel.add(nameLabel);

		nameField = new JTextField(20);
		nameField.setText(name);
		panel.add(nameField);

		emailLabel = new JLabel("Email :");
		panel.add(emailLabel);

		emailField = new JTextField(20);
		emailField.setText(email);
		panel.add(emailField);

		init();
	}

	/**
	 * Constructor
	 * 
	 * @param profile
	 *            user profile
	 * @param msgBundle
	 *            language resource
	 */
	protected AddAddress(Profile profile, ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("AddAddress.frameTitle"), true);

		this.msgBundle = msgBundle;
		this.profile = profile;

		added = false;

		listener = new AddAddressListener();

		panel = new JPanel();

		nameLabel = new JLabel(msgBundle.getString("AddAddress.nameLabelText"));
		panel.add(nameLabel);

		nameField = new JTextField(20);
		panel.add(nameField);

		emailLabel = new JLabel("Email :");
		panel.add(emailLabel);

		emailField = new JTextField(20);
		panel.add(emailField);

		init();
	}

	/** Initialisation method */
	private void init() {
		int nb = profile.getNbSupFields();

		if (profile.getLdapUser() && nb > 0) {
			supLabels = new JLabel[nb];
			supFields = new JTextField[nb];

			String sup[] = profile.getSupFields();

			for (int i = 0; i < nb; i++) {
				StringBuffer fieldName = new StringBuffer(sup[i]);
				fieldName.append(" : ");
				supLabels[i] = new JLabel(fieldName.toString());

				panel.add(supLabels[i]);
				supFields[i] = new JTextField(20);
				panel.add(supFields[i]);
			}
		}

		addXML = new JButton(msgBundle.getString("AddAddress.addXMLLabel"));
		addXML.addActionListener(listener);
		panel.add(addXML);

		addLDAP = new JButton(msgBundle.getString("AddAddress.addLDAPLabel"));
		addLDAP.addActionListener(listener);

		if (!profile.getLdapUser()) {
			addLDAP.setEnabled(false);
		}
		panel.add(addLDAP);

		cancel = new JButton(msgBundle.getString("AddAddress.cancelLabel"));
		cancel.addActionListener(listener);
		panel.add(cancel);

		setContentPane(panel);

		int height = 200 + nb * 50;

		setSize(300, height);
		show();
	}

	/**
	 * Operation confirmation
	 * 
	 * @return boolean saying wether the name/address wad added or not
	 */
	protected final boolean wasAdded() {
		return (added);
	}

	/** Listener for this class */
	private final class AddAddressListener implements ActionListener {
		/**
		 * This method is invoked when an event is triggered
		 * 
		 * @param e
		 *            event
		 */
		public final void actionPerformed(ActionEvent e) {
			JButton b = (JButton) e.getSource();

			if (b == addXML) {
				if (nameField.getText().compareTo("") == 0
						|| emailField.getText().compareTo("") == 0)
					return;

				Entry entry = new Entry(nameField.getText(),
						emailField.getText());
				profile.getAddresses().add(entry);

				added = true;

				hide();
			}

			else if (b == addLDAP) {
				if (nameField.getText().compareTo("") == 0
						|| emailField.getText().compareTo("") == 0) {
					return;
				}
				int nb = profile.getNbSupFields();

				String fields[] = new String[nb];

				for (int i = 0; i < nb; i++)
					fields[i] = supFields[i].getText();

				LdapSearch.addEntry(profile, nameField.getText(),
						emailField.getText(), fields);

				added = true;

				hide();
			} else if (b == cancel)
				hide();
		}
	}
}
